package Prefix_and;

import java.util.HashMap;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-01-26
 * Time:15:55
 * Text：525. 连续数组
 */
public class Text7 {
    public static int findMaxLength(int[] nums) {
        HashMap<Integer,Integer> map=new HashMap<>();
        map.put(0,-1);
        int sum=0;
        int cur=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==0){
                nums[i]=-1;
            }
        } for(int i=0;i<nums.length;i++){
            sum+=nums[i];
            if(map.getOrDefault(sum,-2)!=-2){
                int size=i-map.get(sum);
                if(size>cur){
                    cur=size;
                }
            }else{
                map.put(sum,i);
            }
        }

        return cur;
    }

    public static void main(String[] args) {
        int[] dp=new int[]{0,1};
        System.out.println(findMaxLength(dp));

    }
}
